package com.magicalcoder.lowcode.service.apiopen.service;

import com.magicalcoder.lowcode.service.apiopen.dto.McPageData;

import java.util.List;
import java.util.Map;

public interface MagicalCoderJpaService {
    /**
     * 根据主键获取某个表的数据
     * @param tableName
     * @param id
     * @param <T>
     * @return
     */
     <T> T get(String tableName, Object id);

    /**
     * 多库场景：根据更多信息获取某个表的实体
     * @param catalog
     * @param schema
     * @param tableName
     * @param id
     * @param <T>
     * @return
     */
     <T> T get(String catalog, String schema, String tableName, Object id);

    /**
     * 根据主键删除某个数据
     * @param tableName
     * @param id
     */
     void delete(String tableName, Object id);
     <T> void delete(T entity);
     /**
     * 根据主键删除某个数据
     * @param catalog
     * @param schema
     * @param tableName
     * @param id
     */
     void delete(String catalog, String schema, String tableName, Object id);

     int remove(String tableName, Map entity);
     <T> int remove(T entity);
     int remove(String catalog, String schema, String tableName, Map entity);
    /**
     * 保存实体 如果不传主键 则insert 否则update
     * @param tableName
     * @param entity
     */
    <T> T insert(String tableName, Map entity);
    /**
     * 保存实体 如果不传主键 则insert 否则update
     * @param entity
     */
    <T> T insert(T entity);
    /**
     * 保存实体 如果不传主键 则insert 否则update
     * @param catalog
     * @param schema
     * @param tableName
     * @param entity
     */
    <T> T  insert(String catalog, String schema, String tableName, Map entity);
    /**
     * 保存实体 如果不传主键 则insert 否则update
     * @param tableName
     * @param entity
     */
    <T> T  save(String tableName, Map entity);
    <T> T  save(T entity);
    /**
     * 保存实体 如果不传主键 则insert 否则update
     * @param catalog
     * @param schema
     * @param tableName
     * @param entity
     */
    <T> T  save(String catalog, String schema, String tableName, Map entity);

    <T> T  update(String tableName, Map entity);
    <T> T  update(T entity);
    <T> T  update(String catalog, String schema, String tableName, Map entity);


    /**
     * 批量插入
     * @param tableName
     * @param entityList
     */
    <T> List<T> batchInsert(String tableName, List<Map> entityList);
    <T> List<T> batchInsert(List<T> entityList);
    /**
     * 批量插入
     * @param catalog
     * @param schema
     * @param tableName
     * @param entityList
     */
    <T> List<T> batchInsert(String catalog, String schema, String tableName, List<Map> entityList);
    /**
     * 批量删除
     * @param tableName
     * @param ids
     */
    void batchDelete(String tableName, List<Object> ids);
    <T> void batchDelete(List<T> entityList);
    void batchDelete(String catalog, String schema, String tableName, List<Object> ids);
    /**
     * 批量保存
     * @param tableName
     * @param entityList
     */
    <T> List<T> batchSave(String tableName, List<Map> entityList);
    <T> List<T> batchSave(List<T> entityList);

    /**
     * 批量保存
     * @param entityList
     */
    <T> List<T> batchSave(String catalog, String schema, String tableName, List<Map> entityList);
    <T> List<T> batchDeleteAndSave(String tableName, List<Map> entityList, List<Map> deleteList);
    <T> List<T> batchDeleteAndSave(String catalog, String schema, String tableName, List<Map> entityList, List<Map> deleteList);
    <T> List<T> batchUpdate(String catalog, String schema, String tableName, List<Map> entityList);
    /**
     * 会校验mcQuery
     * @param tableName
     * @param mcQuery
     * @param <T>
     * @return
     */
    <T> List<T> find(String tableName, Map<String, Object> mcQuery, Integer page, Integer limit);
    <T> List<T> find(String catalog, String schema, String tableName, Map<String, Object> mcQuery, Integer page, Integer limit);

    /**
     * 不会校验McQuery
     * @param tableName
     * @param mcQuery
     * @param <T>
     * @return
     */
    <T> List<T> find(String tableName, Map<String, Object> mcQuery);
    <T> List<T> find(String catalog, String schema, String tableName, Map<String, Object> mcQuery);

    /**
     * 会校验mcQuery
     * @param tableName
     * @param mcQuery
     * @param page
     * @param limit
     * @param <T>
     * @return
     */
    <T> McPageData<T> pager(String tableName, Map<String, Object> mcQuery, Integer page, Integer limit);
    <T> McPageData<T> pager(String catalog, String schema, String tableName, Map<String, Object> mcQuery, Integer page, Integer limit);

    /**
     * 不会校验McQuery
     * @param tableName
     * @param mcQuery
     * @param <T>
     * @return
     */
    <T> McPageData<T> pager(String tableName, Map<String, Object> mcQuery);
    <T> McPageData<T> pager(String catalog, String schema, String tableName, Map<String, Object> mcQuery);

    /**
     * 自定义hql查询
     * @param hql
     * @param query 查询条件
     * @return
     */
    List findHql(String hql, Map<String, Object> query);
    long countHql(String hql, Map<String, Object> query);
    int updateHql(String hql, Map<String, Object> query);

    /**
     * 分页自定义查询
     * @param hql
     * @param query
     * @param page
     * @param limit
     * @return
     */
    List findHql(String hql, Map<String, Object> query, Integer page, Integer limit);
    /**
     * 自定义hql查询
     * @param hql
     * @param query 查询条件
     * @return
     */
    <T> McPageData<T> pagerHql(String hql, Map<String, Object> query);

    /**
     * 分页自定义查询
     * @param hql
     * @param query
     * @param page
     * @param limit
     * @return
     */
    <T> McPageData<T> pagerHql(String hql, Map<String, Object> query, Integer page, Integer limit);

    /**
     * 清理父表及关联过的所有子表详情
     * {id:1,name:'',_info:{catalog:'c',schema:null,table:'t'},_children:[{catalog:'c',schema:null,table:'t',pidName:'parentId'}]}
     */
    Map<String,Object> removeParentChildren(Map<String, Object> parent);
    /**
     * 保存父子表详情
     * {id:1,name:'',_info:{catalog:'c',schema:null,table:'t'},_children:[{_info:{catalog:'c',schema:null,table:'t',pidName:'parentId'},saveList:[],deleteList:[]}]}
     */
    Map<String,Object> saveParentChildren(Map<String, Object> parent);
}
